package cn.com.elite.ivrsms;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;

import cn.com.elite.mail.MailSend;

import com.elite.jmetadata.JMetaData;
import com.elite.jmetadata.JMetaDataStorage;
import com.elite.si.JVRUFlow;
import com.elite.si.JVRUFlow.treatresult;
import com.elite.util.MetaDataSocketClient;

public class IvrSms {
	private static final Log log=LogFactory.getLog(IvrSms.class);
	public String Elitevruip;
	public int Elitevruport;
	public int Ibchannelid;
	public int Treatmenttimeout;

//	private final int CMD_ANSWERCALL=204;
//	private final int CMD_TRANSFERCALL=207;
//	private final int CMD_RELEASECALL=206;
//	private final int VRUEVENT=0x1001;
//	private final int E_EVT_CHANNEL_OPANSWERED=1109;
//	private final int E_EVT_CHANNEL_OPDISCONNECTED=1110;
//	private final int E_EVT_CHANNEL_TPDISCONNECTED=1107;
//	private final int E_EVT_CHANNEL_RINGING=1105;
//	
//	private int ibchannelid; //对应一个WF_CHANNEL中的通道
//	private String elitevruip; //对应EliteVRU的IP地址
//	private int elitevruport=8001; //对应EliteVRU的端口
//	private int treatmenttimeout=30;//默认30秒超时
//	private boolean running=false; //控制退出
//	private Thread flowthread=null;
//	private int currentresult=0;
//	private String handleby=null;
//	private String handlegroup=null;
//	private boolean changehandleby=false;
//	private int hclient=-1;
	
	private MetaDataSocketClient socketclient=new MetaDataSocketClient();
	
	public IvrSms(String ip,int port,int channelid,int timeout){
		Elitevruip=ip;
		Elitevruport=port;
		Ibchannelid=channelid;
		Treatmenttimeout=timeout;
	}
	public String treatSMS(String documentin_guid,String documentfrom,String documentto,String content,String handlegroup,String handleby)throws Exception{
		JVRUFlow flow=new JVRUFlow();
		flow.setElitevruip("127.0.0.1");
		flow.setElitevruport(8001);
		flow.setIbchannelid(1001);
		flow.setTreatmenttimeout(30);
		flow.init();
		treatresult ret=flow.HandleIBSMS(documentfrom,documentto,content, handlegroup, handleby);
		String sql="";
		if(ret.result==0){
			sql=" update documentin set status=1 where documentin_guid='"+documentin_guid+"'";
		}else if(ret.result==1){
			sql=" update documentin set status=2 where documentin_guid='"+documentin_guid+"'";
		}else if(ret.result==2){
			sql=" update documentin set status=4 where documentin_guid='"+documentin_guid+"'";
		}else if(ret.result==3){
			sql=" update documentin set status=2 where documentin_guid='"+documentin_guid+"'";
		}else{
			log.error("IVR返回错误："+ret.result);
		}
		return sql;

	}
//	public class treatresult{
//		//return 3 means take this message as treated failed(status=2)
//		//return 2 means take this message as treated successfully(status=4)
//		//return 1 means do not treat this message again(废弃)
//		//return 0 means treat this message as normal
//		public int result;
//		public String handlegroup;
//		public String handleby;
//	}
//	public treatresult HandleIBSMS(String cFrom, String cTo, String cContent,String strHandleGroup,String strHandleBy){
//		treatresult ret=new treatresult();
//		ret.result=0;
//		ret.handleby=strHandleBy;
//		ret.handlegroup=strHandleGroup;
//		this.notifyAll();
//		if(socketclient.isConnected()){
//			JMetaDataStorage pMessage=new JMetaDataStorage();
//			pMessage.AddData(new JMetaData("Event",E_EVT_CHANNEL_RINGING));
//			pMessage.AddData(new JMetaData("DeviceID",ibchannelid));
//			JMetaDataStorage AttachData=new JMetaDataStorage();
//			AttachData.AddData(new JMetaData("DialedNumber",cTo));
//			AttachData.AddData(new JMetaData("CallerID",cFrom));
//			AttachData.AddData(new JMetaData("Content",cContent));
//			pMessage.AddData(new JMetaData("AttachData",AttachData));
//			handlegroup="";
//			handleby="";
//			changehandleby=false;
//			if(socketclient.SendMessage(VRUEVENT,pMessage)){
//				currentresult=0;
//				try {
//					this.wait(treatmenttimeout*1000);
//				} catch (InterruptedException e) {
//					// TODO Auto-generated catch block
//					log.error("",e);
//				}
//				ret.result=currentresult;
//				switch(ret.result){
//				case 0:
//					log.debug("Flow engine return CONTINUE_NORMAL treatment");
//					if(changehandleby){
//						ret.handlegroup=handlegroup;
//						ret.handleby=handleby;
//					}
//					break;
//				case 1:
//					log.debug("Flow engine return CONTINUE_IGNORE treatment");
//					break;
//				case 2:
//					log.debug("Flow engine return CONTINUE_CLOSED treatment");
//					break;
//				case 3:
//					log.debug("Flow engine return CONTINUE_FAILED treatment");
//					break;
//				}
//			}
//		}
//		return ret;
//		
//	}

}
